package com.yunst.net;

import com.yunst.net.handle.ChannelInitializerHandle;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;

/**
 * @author yunst
 * @date 2022/12/30
 */
public class NetClient {
    private String ipAddress;
    private int port = 8088;

    public NetClient(String ip) {
        this.ipAddress = ip;
        init();
    }

    public void init() {
        NioEventLoopGroup group = new NioEventLoopGroup();

        try {
            Bootstrap bootstrap = new Bootstrap()
                    .group(group)
                    .channel(NioSocketChannel.class)
                    .handler(new ChannelInitializerHandle());

            Channel channel = bootstrap.connect(ipAddress, port).sync().channel();
            channel.closeFuture().sync();

        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            group.shutdownGracefully();
        }
    }
}
